


if(request.method!=='POST'){

	
	response.forward("/register/register.jst",request,response);
	response.end();

}else{





	
	//if ajax username check handle first
	var isAjaxUserNamecheck=request.parameters["precheck"];
	if(isAjaxUserNamecheck&&isAjaxUserNamecheck==="true"){
	
		var username=request.parameters["username"];
	
		var authController=request.require("./authController");
		authController.checkUsername(username,response,userNamecb);
	
	}
	else
	{
	

		var username=request.parameters["username"];
		var password=request.parameters["password"];
		var name=request.parameters["name"];
		var picture=request.parameters["picture"];

		if(!validateParams(request,response)){
	
			return;
		}

	
		var authController=request.require("./authController");

		var cb=function (user,msg){


				var loggedIn=false;	
				if(typeof user==='undefined'){
	
					msgs=[msg];
					request.setAttribute("errorMsgs",msgs);
					request.method='GET';
					response.forward("/register/register.jst",request,response);
					response.end();

				}else{
	
		
					var session=request.getSession();
					session.LoggedIn=true;
					session.user={
						username:user["username"],
						name:user["name"],
						picture:user["picture"]
				
					};

					response.redirect("/home",request,response);
					response.end();
				}
	
	
			};






		authController.TryRegister(username,password,name,picture,cb);
	}
}


function validateParams(request,response){

		var username=request.parameters["username"];
		var password=request.parameters["password"];
		var name=request.parameters["name"];
		var picture=request.parameters["picture"];

		var errorMsgs=new Array();

		if(!username||username.length<1){
		
			errorMsgs.push("please enter a username");
		}
		if(!password||password.length<1){
		
			errorMsgs.push("please enter a password");
		}
		if(!name||name.length<1){
		
			errorMsgs.push("please enter a name");
		}
		if(!picture||picture.length<1){
		
			errorMsgs.push("please choose a picture");
		}

		if(errorMsgs.length>0){
		
			request.setAttribute("errorMsgs",errorMsgs);
			request.method='GET';
			response.forward("/register/register.jst",request,response);
			response.end();
		
			return false;
		}


		return true;
}







function userNamecb(user,response){

	if(typeof user==='undefined'){
		var output = {nameOk: true,msg:"username is valid"};
	
		response.headers["Content-Type"]="application/json";
		response.headers["Access-Control-Allow-Origin"]="*";
		response.body="";
		response.body+=JSON.stringify(output);
		response.end();

	 
	}

	else{
	
		var output = {nameOk: false,msg:"username is taken"};
	
		response.headers["Content-Type"]="application/json";	
		response.headers["Access-Control-Allow-Origin"]="*";
		response.body="";
		response.body+=JSON.stringify(output);
		response.end();
		
	


	}
	
	
}



